package com.flash.DayProcessing.controller;

import com.flash.DayProcessing.entity.AccountValue;
import com.flash.DayProcessing.entity.SecurityMessage;
import com.flash.DayProcessing.entity.ValusStatistical;
import com.flash.DayProcessing.service.AccountValueSerice;
import com.flash.DayProcessing.service.SecurityMessageService;
import com.flash.DayProcessing.service.TaStatisticsService;
import com.flash.DayProcessing.service.ValusStatisticalService;
import com.flash.InventoryManagement.entity.TaStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 这是净值统计的控制层
 * @author Liy
 * @since 2022-8-24
 * @version 1.0
 */
@RestController
public class ValueStatisticalController {
    @Resource
    ValusStatisticalService valusStatisticalService;
    @Resource
    SecurityMessageService securityMessageService;
    @Resource
    AccountValueSerice accountValueSerice;
    @Resource
    TaStatisticsService taStatisticsService;


    /**
     * 统计按钮，先删后增
     * @param statisticsDate 输入的时间
     * @param request 获得基金id，现金id
     * @return map 将数据发送到网页上
     */
    @RequestMapping("insertValusStatistical")
    public Map insertValusStatistical(String statisticsDate, HttpServletRequest request) throws ParseException {
        System.out.println("这是先删后增的方法");
        //债券利息
        double num=0;
        //查询证券清算款-流入（应收）
        double num1=0;
        //查询证券清算款-流出（应付）
        double num2=0;
        //主账户TA清算款-应收
        double num3=0;
        //主账户TA清算款-应付
        double num4=0;
        //次账户TA清算款-应收
        double num5=0;
        //次账户TA清算款-应付
        double num6=0;
        //主账户管理费
        double num7=0;
        //主账户托管费
        double num8=0;
        //次账户管理费
        double num9=0;
        //次账户托管费
        double num10=0;
        //合计估值增值
        double num11=0;
        //合计总资产
        double num13=0;
        //单位资产净值
        double num14=0;

        //获得基金id
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        String cashId = (String) session.getAttribute("cashId");

        //去空格
        statisticsDate = statisticsDate.trim();
        fundId = fundId.trim();
        cashId = cashId.trim();

        //先删除后增加
        valusStatisticalService.deleteStatistical(statisticsDate,fundId);
        String time = statisticsDate;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df.parse(statisticsDate);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        i--;
        if(i==6){
            instance.add(Calendar.DATE, -1); //得到前一天
            Date dateTimeTwo = instance.getTime();
            time=df.format(dateTimeTwo);

        }else if(i==7){
            instance.add(Calendar.DATE, -2); //得到前一天
            Date dateTimeTwo = instance.getTime();
            time=df.format(dateTimeTwo);
        }
        time=time.trim();

        //查询证券模块
        List<SecurityMessage> securityMessages = securityMessageService.selectSecrityMessage(time, fundId, statisticsDate);
        //查询债券利息
        List<SecurityMessage> securityMessages2 = securityMessageService.selectDebentureInterestValueStatistics(statisticsDate, fundId, 3);

        //合计债券利息
        if(securityMessages2.size()!=0) {
            for (SecurityMessage valueStatistics : securityMessages2) {
                if(valueStatistics!=null){
                    num = num + valueStatistics.getTotalPrice();
                }
            }
        }

        //合计证券清算款所有流入
        List<SecurityMessage> securityMessages3 = securityMessageService.selectSecuritiesClearingValueStatistics(statisticsDate, fundId, 2, 1);
        if(securityMessages3.size()!=0){
            for (SecurityMessage valueStatistics : securityMessages3) {
                if(valueStatistics!=null) {
                    num1 = num1 + valueStatistics.getTotalPrice();
                }
            }
        }


        //合计证券清算款所有流出
        List<SecurityMessage> securityMessages4 = securityMessageService.selectSecuritiesClearingValueStatistics(statisticsDate, fundId, 2,  -1);
        if(securityMessages4.size()!=0){
            for (SecurityMessage valueStatistics : securityMessages4) {
                if(valueStatistics!=null){
                    num2=num2+valueStatistics.getTotalPrice();
                }
            }
        }

        //递增功能ID
        int id=3;
        //数据块插入证券模块外层对象
        ValusStatistical valusStatistical1 = new ValusStatistical(1,-1,fundId,"证券",statisticsDate);
        valusStatisticalService.insertStatistical(valusStatistical1);
        if(securityMessages.size() != 0){
            //数据块插入股票模块外层对象
           ValusStatistical valusStatistical2 = new ValusStatistical(2,1,fundId,"股票",statisticsDate);
           valusStatisticalService.insertStatistical(valusStatistical2);

            if (securityMessages.size()!=0) {
                //遍历证券模块数据集合
                for (SecurityMessage securityMessage:securityMessages) {
                    //判断证券模块是否为null
                    if (securityMessages!=null) {
                        //判断是否为股票
                        if (securityMessage.getSecurityType().equals("股票")) {
                            //添加股票
                            valusStatisticalService.insertStatistical(new ValusStatistical(id,2,fundId,securityMessage.getSecurityName(),securityMessage.getSecurityId(),securityMessage.getQuantity(),securityMessage.getClosingPrice(),securityMessage.getMoney(),securityMessage.getMarketValue(),securityMessage.getTotalPrice(),statisticsDate));
                            //id++
                            id++;
                        }
                    }
                }
            }


            //数据块插入债券模块外层对象
            ValusStatistical valusStatistical3 = new ValusStatistical(id,1,fundId,"债券",statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical3);
            //递增功能ID
            id++;
            if (securityMessages.size() != 0) {
                //遍历证券模块数据集合
                for (SecurityMessage securityMessage : securityMessages) {
                    //判断证券模块是否为null
                    if (securityMessage != null) {
                        //合计总市值
                        num13 = num13 + securityMessage.getMarketValue();
                        //合计证券模块估值增值
                        num11 = num11 + securityMessage.getTotalPrice();
                        if (securityMessage.getSecurityType().equals("债券")) {
                            //添加债券
                            valusStatisticalService.insertStatistical(new ValusStatistical(id, valusStatistical3.getProjectId(), fundId, securityMessage.getSecurityName(), securityMessage.getSecurityId(), securityMessage.getQuantity(), securityMessage.getClosingPrice(), securityMessage.getMoney(), securityMessage.getMarketValue(), securityMessage.getTotalPrice(), statisticsDate));
                            //id++
                            id++;
                        }
                    }
                }
            }
        }

        //查询现金模块及现金利息
        List<AccountValue> accountValues = accountValueSerice.selectAccountValue(statisticsDate, fundId, 3);
        System.out.println(accountValues+"aaaaa");

        //数据块插入现金模块外层对象
        ValusStatistical valusStatistical4=new ValusStatistical(id,-1,fundId,"现金",statisticsDate);
        valusStatisticalService.insertStatistical(valusStatistical4);
        //递增功能ID
        id++;
        System.out.println("现金id是："+cashId);

        //判断现金模块是否等于0
        if (accountValues.size()!=0){
            //遍历现金模块数据集合
            for (AccountValue accountValue:accountValues){
                //判断现金模块是否为空
                if (accountValue!=null){
                    //获得两个账户总余额+现金利息 num13
                    num13 = num13 + accountValue.getMoney() + accountValue.getTotalMoney();
                    //判断当前现金id是否等于现金模块中的id
                    if(cashId.equals(accountValue.getCashId())){
                        //数据库插入主账户基本数据及余额
                        ValusStatistical valusStatistical5 = new ValusStatistical(id,valusStatistical4.getProjectId(),fundId,accountValue.getAccountName(),accountValue.getBankId(),accountValue.getMoney(),statisticsDate);
                        valusStatisticalService.insertStatistical(valusStatistical5);
                        //id++
                        id++;

                        //数据库插入主账户存款利息
                        ValusStatistical valusStatistical6 = new ValusStatistical(id,valusStatistical5.getProjectId(), fundId,"存款利息", accountValue.getTotalMoney(), statisticsDate);
                        valusStatisticalService.insertStatistical(valusStatistical6);
                        //id++
                        id++;

                        //数据库插入主账户债券利息
                        //判断债券利息是否为0 num
                        if(num!=0){
                            ValusStatistical valusStatistical7 = new ValusStatistical(id,valusStatistical5.getProjectId(), fundId,"债券利息", num, statisticsDate);
                            valusStatisticalService.insertStatistical(valusStatistical7);
                            //id++
                            id++;
                        }

                        //数据库插入主账户证券清算款 num1 num2
                        if((num1-num2)!=0) {
                            ValusStatistical valusStatistical8 = new ValusStatistical(id,valusStatistical5.getProjectId(), fundId,"证券清算款", num1-num2, statisticsDate);
                            valusStatisticalService.insertStatistical(valusStatistical8);
                            //id++
                            id++;
                        }

                        //Ta应收
                        List<AccountValue> accountValues2 = accountValueSerice.selectAccountTa(accountValue.getCashId(), 4, 1, statisticsDate, fundId);

                        //判断ta应收是否为0
                        if(accountValues2.size()!=0) {
                            //遍历ta应收
                            for (AccountValue accountValue1 : accountValues2) {
                                //判断集合是否为null
                                if (accountValue1 != null) {
                                    //合计TA应收 num3
                                    num3 = num3 + accountValue1.getTotalMoney();
                                }
                            }
                        }


                        //Ta应付
                        List<AccountValue> accountValues3 = accountValueSerice.selectAccountTa(accountValue.getCashId(), 4, -1, statisticsDate, fundId);

                        //判断ta应付是否为0
                        if(accountValues3.size()!=0) {
                            //遍历ta应付
                            for (AccountValue accountValue2 : accountValues3) {
                                ////判断集合是否为null
                                if (accountValue2 != null) {
                                    //合计TA应收 num4
                                    num4 = num4 + accountValue2.getTotalMoney();
                                }
                            }
                        }

                        //数据库插入主账户TA清算款 num3 num4
                        if((num3 - num4)!=0) {
                            ValusStatistical valusStatistical9= new ValusStatistical(id,valusStatistical5.getProjectId(), fundId,"TA清算款", num3 - num4, statisticsDate);
                            valusStatisticalService.insertStatistical(valusStatistical9);
                            //id++
                            id++;
                        }

                        //根据主账户ID查询管理费
                        List<AccountValue> accountValues4= accountValueSerice.selectOperationCost(accountValue.getCashId(), statisticsDate, fundId, 1);

                        //得到主账户管理费
                        if(accountValues4.size()!=0){
                            //遍历主账户管理费
                            for (AccountValue accountValue2:accountValues4){
                                //判断对象是否为null
                                if (accountValue2!=null){
                                    //拿到主账户管理费 num7
                                    num7 = accountValue2.getMoney();
                                }
                            }
                        }

                        //根据主账户ID查询托管费
                        List<AccountValue> accountValues5= accountValueSerice.selectOperationCost(accountValue.getCashId(), statisticsDate, fundId, 2);

                        //得到主账户托管费
                        if(accountValues5.size()!=0){
                            //遍历主账户托管费
                            for (AccountValue accountValue3:accountValues5){
                                //判断对象是否为null
                                if (accountValue3!=null){
                                    //拿到主账户托管费 num8
                                    num8 = accountValue3.getMoney();
                                }
                            }
                        }
                    }
                }
            }
        }

        //再次遍历现金模块
        //判断现金模块是否等于0
        if (accountValues.size()!=0){
            //遍历现金模块数据集合
            for (AccountValue accountValue:accountValues){
                //判断现金模块是否为空
                if (accountValue!=null){
                    //判断当前现金id是否等于现金模块中的id
                    if(!cashId.equals(accountValue.getCashId())){
                        //数据库插入次账户基本数据及余额
                        ValusStatistical valusStatistical10 = new ValusStatistical(id,valusStatistical4.getProjectId(),fundId,accountValue.getAccountName(),accountValue.getBankId(),accountValue.getMoney(),statisticsDate);
                        valusStatisticalService.insertStatistical(valusStatistical10);
                        //id++
                        id++;

                        //数据库插入次账户现金利息
                        ValusStatistical valusStatistical11 = new ValusStatistical(id,valusStatistical10.getProjectId(), fundId,"存款利息", accountValue.getTotalMoney(), statisticsDate);
                        valusStatisticalService.insertStatistical(valusStatistical11);
                        //id++
                        id++;

                        //Ta应收
                        List<AccountValue> accountValues2 = accountValueSerice.selectAccountTa(accountValue.getCashId(), 4, 1, statisticsDate, fundId);

                        //判断ta应收是否为0
                        if(accountValues2.size()!=0) {
                            //遍历ta应收
                            for (AccountValue accountValue1 : accountValues2) {
                                //判断集合是否为null
                                if (accountValue1 != null) {
                                    //合计TA应收  num5
                                    num5 = num5 + accountValue1.getTotalMoney();
                                }
                            }
                        }

                        //Ta应付
                        List<AccountValue> accountValues3 = accountValueSerice.selectAccountTa(accountValue.getCashId(), 4, -1, statisticsDate, fundId);

                        //判断ta应付是否为0
                        if(accountValues3.size()!=0) {
                            //遍历ta应付
                            for (AccountValue accountValue2 : accountValues3) {
                                //判断集合是否为null
                                if (accountValue2 != null) {
                                    //合计TA应付  num6
                                    num6 = num6 + accountValue2.getTotalMoney();
                                }
                            }
                        }

                        //数据库插入次账户TA清算款  num5 num6
                        if((num5 - num6)!=0) {
                            ValusStatistical valusStatistical12= new ValusStatistical(id,valusStatistical10.getProjectId(), fundId,"TA清算款", num5 - num6, statisticsDate);
                            valusStatisticalService.insertStatistical(valusStatistical12);
                            //id++
                            id++;
                        }

                        //根据次账户ID查询管理费
                        List<AccountValue> accountValues4= accountValueSerice.selectOperationCost(accountValue.getCashId(), statisticsDate, fundId, 1);

                        //得到次账户管理费
                        if(accountValues4.size()!=0){
                            //遍历次账户管理费
                            for (AccountValue accountValue2:accountValues4){
                                //判断对象是否为null
                                if (accountValue2!=null){
                                    //拿到次账户管理费 num9
                                    num9 = accountValue2.getMoney();
                                }
                            }
                        }

                        //根据次账户ID查询托管费
                        List<AccountValue> accountValues5= accountValueSerice.selectOperationCost(accountValue.getCashId(), statisticsDate, fundId, 2);

                        //得到次账户托管费
                        if(accountValues5.size()!=0){
                            //遍历次账户托管费
                            for (AccountValue accountValue3:accountValues5){
                                //判断对象是否为null
                                if (accountValue3!=null){
                                    //拿到次账户托管费 num10
                                    num10 = accountValue3.getMoney();
                                }
                            }
                        }
                    }
                }
            }
        }

        //数据库插入运营模块外层对象
        ValusStatistical valusStatistical13 = new ValusStatistical(id,-1,fundId,"运营",statisticsDate);
        valusStatisticalService.insertStatistical(valusStatistical13);
        //id++
        id++;

        //数据库插入运营模块管理费 主加次 num7 num9
        if((num7+num9)!=0){
            ValusStatistical valusStatistical14 = new ValusStatistical(id,valusStatistical13.getProjectId(), fundId,"管理费", num7 + num9, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical14);
            //id++
            id++;
        }

        //数据库插入运营模块托管费 加次 num8 num10
        if((num8+num10)!=0){
            ValusStatistical valusStatistical15 = new ValusStatistical(id,valusStatistical13.getProjectId(), fundId,"托管费", num8 + num10, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical15);
            //id++
            id++;
        }

        //数据库插入合计模块外层对象
        ValusStatistical valusStatistical16 = new ValusStatistical(id,-1,fundId,"合计",statisticsDate);
        valusStatisticalService.insertStatistical(valusStatistical16);
        //id++
        id++;

        //数据库插入合计模块估值增值数据 num11
        if(num11!=0){
            ValusStatistical valusStatistical17 = new ValusStatistical(id,valusStatistical16.getProjectId(), fundId,"估值增值", num11, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical17);
            //id++
            id++;
        }

        //合计总负债
        double num12 = num2+num4+num6+num7+num8+num9+num10;
        if (num12!=0){
            ValusStatistical valusStatistical18 = new ValusStatistical(id,valusStatistical16.getProjectId(), fundId,"总负债", num12, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical18);
            //id++
            id++;
        }

        //合计总资产
        num13 = num13+num+num1+num3+num5;
        if (num13!=0){
            ValusStatistical valusStatistical19 = new ValusStatistical(id,valusStatistical16.getProjectId(), fundId,"总资产", num13, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical19);
            //id++
            id++;
        }

        //数据库插入合计模块资产净值数据 总资产减负债
        if((num13-num12)!=0){
            ValusStatistical valusStatistical19 = new ValusStatistical(id,valusStatistical16.getProjectId(), fundId,"资产净值", num13-num12, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical19);
            //id++
            id++;
        }

        //查询ta库存数量
        double sum=0;
        List<TaStock> taStocks = taStatisticsService.selectTaNum(fundId, statisticsDate);
        if(taStocks.size()!=0){
            for(TaStock taStock:taStocks){
                if(taStock!=null){
                    sum=sum+taStock.getQuantity();
                }
            }
        }

        //算出单位资产净值
        if(sum!=0){
            //资产（总资产减负债）-数量
            num14=(num13-num12)/sum;
        }

        //数据库插入合计单位资产净值
        if(num14!=0){
            ValusStatistical valusStatistical20 = new ValusStatistical(id,valusStatistical16.getProjectId(), fundId,"单位净值", num14, statisticsDate);
            valusStatisticalService.insertStatistical(valusStatistical20);
            //id++
            id++;
        }

        //通过日期+基金id来查询净值统计集合
        List<ValusStatistical> valusStatisticalList = valusStatisticalService.selectStatisticalDate(statisticsDate, fundId);

        HashMap map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",valusStatisticalList);
        return map;
    }

    /**
     * 查询所有
     * @return 返回一个map发送到网页
     */
    @GetMapping("/valusStatisticalSelect")
    public Map valusStatisticalSelect(){
        System.out.println("这是查询所有净值统计的方法");
        Map map = new HashMap();
        List<ValusStatistical> valusStatisticalList=valusStatisticalService.selectStatistical();
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",valusStatisticalList);
        return map;
    }

    /**
     * 通过日期+基金id来查询净值统计
     * @param statisticsDate 日期
     * @param request 通过request获得session拿到fundId
     * @return 返回净值统计对象的集合
     */
    @GetMapping("/valusStatisticalSelectDate")
    public Map valusStatisticalSelectDate(String statisticsDate, HttpServletRequest request){
        System.out.println("这是通过日期来查询的方法");

        //去掉日期后面的空格
        String statisticsDate2 = statisticsDate.trim();
        //通过session拿到fundId
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");

        //通过日期+基金id来查询净值统计集合
        List<ValusStatistical> valusStatisticalList = valusStatisticalService.selectStatisticalDate(statisticsDate2, fundId);
        System.out.println(valusStatisticalList);

        Map map = new HashMap();

        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",valusStatisticalList);
        return map;
    }
}
